package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemGsChangeContentBinding;
import com.example.yichuangyun.databinding.ItemGsChangeTopDateBinding;
import com.example.yichuangyun.model.company.GsChangeRecordDataModel;
import com.example.yichuangyun.utils.DensityUtil;
import com.example.yichuangyun.utils.StrCompareColorTool;

import java.util.List;

/**
 * 工商变更
 */
public class GsChangeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<GsChangeRecordDataModel.ChangeRecordDataModel> data;

    public GsChangeAdapter(Context context, List<GsChangeRecordDataModel.ChangeRecordDataModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemGsChangeTopDateBinding binding = ItemGsChangeTopDateBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            GsChangeRecordDataModel.ChangeRecordDataModel model = data.get(position);
            myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getChangeRecords(), position));
            myViewHolder.binding.tvDate.setText(model.getDate());
            myViewHolder.binding.topLine.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemGsChangeTopDateBinding binding;

        public MyViewHolder(ItemGsChangeTopDateBinding itemGsChangeTopDateBinding) {
            super(itemGsChangeTopDateBinding.getRoot());
            binding = itemGsChangeTopDateBinding;
        }
    }

    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final List<GsChangeRecordDataModel.ChangeRecordModel> mData;
        private final int parentPosition;

        public MyAdapter(List<GsChangeRecordDataModel.ChangeRecordModel> mData, int parentPosition) {
            this.mData = mData;
            this.parentPosition = parentPosition;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ItemGsChangeContentBinding binding = ItemGsChangeContentBinding.inflate(LayoutInflater.from(context), parent, false);
            return new ChangeViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof ChangeViewHolder) {
                ChangeViewHolder myViewHolder = (ChangeViewHolder) holder;
                GsChangeRecordDataModel.ChangeRecordModel model = mData.get(position);
                myViewHolder.binding.tvChangeTitle.setText(model.getChangeItem());
                if (TextUtils.isEmpty(model.getAfterContent()) && TextUtils.isEmpty(model.getBeforeContent())) {
                    myViewHolder.binding.tvChangeAfter.setText("");
                    myViewHolder.binding.tvChangeBefore.setText("");
                }
                if (TextUtils.isEmpty(model.getAfterContent()) && !TextUtils.isEmpty(model.getBeforeContent())) {
                    myViewHolder.binding.tvChangeAfter.setText("");
                    String text = String.format(context.getString(R.string.gs_change_content), model.getBeforeContent());
                    myViewHolder.binding.tvChangeBefore.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
                }
                if (TextUtils.isEmpty(model.getBeforeContent()) && !TextUtils.isEmpty(model.getAfterContent())) {
                    String text = String.format(context.getString(R.string.gs_change_content), model.getAfterContent());
                    myViewHolder.binding.tvChangeAfter.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
                    myViewHolder.binding.tvChangeBefore.setText("");
                }

                String s1;
                String s2;
                if (!TextUtils.isEmpty(model.getBeforeContent()) && !TextUtils.isEmpty(model.getAfterContent())){
                    if (model.getAfterContent().length() > model.getBeforeContent().length()) {
                        s1 = StrCompareColorTool.compareStrShort(model.getAfterContent(), model.getBeforeContent(), "#FF0000");
                        s2 = StrCompareColorTool.compareStrLong(model.getAfterContent(), model.getBeforeContent(), "#FF0000");
                    } else {
                        s1 = StrCompareColorTool.compareStrLong(model.getAfterContent(), model.getBeforeContent(), "#FF0000");
                        s2 = StrCompareColorTool.compareStrShort(model.getAfterContent(), model.getBeforeContent(), "#FF0000");
                    }

                    myViewHolder.binding.tvChangeBefore.setText(Html.fromHtml(s1, Html.FROM_HTML_MODE_LEGACY));
                    myViewHolder.binding.tvChangeAfter.setText(Html.fromHtml(s2, Html.FROM_HTML_MODE_LEGACY));
                }
                myViewHolder.binding.llLine.setPadding(0, 0, 0, parentPosition == data.size() - 1 && position == mData.size() - 1 ? DensityUtil.dip2px(context, 20) : 0);
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }

    static class ChangeViewHolder extends RecyclerView.ViewHolder {

        ItemGsChangeContentBinding binding;

        public ChangeViewHolder(ItemGsChangeContentBinding itemGsChangeContentBinding) {
            super(itemGsChangeContentBinding.getRoot());
            binding = itemGsChangeContentBinding;
        }
    }
}
